package com.ropeok.controller;

import com.ropeok.Result.Result;
import com.ropeok.entity.RoleAndPermission;
import com.ropeok.entity.User;
import com.ropeok.mapper.UserMapper;
import com.ropeok.service.serviceimpl.ServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Api(tags = "操作用户表")
@RestController
public class UserController {

    @Autowired
    UserMapper userMapper;
    @Autowired
    ServiceImpl service;

    @ApiOperation(value = "增加用户",notes = "增加用户信息")
    @PostMapping(value = "/addUser")
    public Result SaveUser(@Valid User user, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        service.SaveUser(user);
        return Result.success();
    }


    @ApiOperation(value = "查找信息",notes = "查找全部信息")
    @GetMapping(value = "/Userlist")
    public Result UserList(){
        List<User> list = service.list();
        return new Result().success(list);
    }
    @ApiOperation(value = "删除角色信息",notes = "删除角色信息")
    @GetMapping(value = "/Deluser")
    public Result DelUser(Integer id){
        Result result;
        if (null==id){
            result=Result.error("id不能为空");
        }else {
            service.DelUser(id);
            result=Result.success("操作成功");
        }
        return result;
    }

    @ApiOperation(value = "更新信息",notes = "更新信息")
    @PostMapping(value = "/update")
    public Result updateUser(User user){
        Result result;
        if(null==user){
            result=Result.error("请填入相应信息");
        }else {
            userMapper.UpdateUser(user);
            result=Result.success("操作成功");
        }
        return result;
    }
}
